/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Inject;
import javax.inject.Named;

import ar.com.greensoft.jeecommons.web.jsf.export.ColumnValueMapper;
import ar.com.greensoft.jeecommons.web.jsf.export.ExportColInfo;
import ar.com.greensoft.jeecommons.web.jsf.export.impl.ExportColInfoImpl;
import ar.com.greensoft.jeecommons.web.jsf.util.HttpJSFUtil;
import ar.com.greensoft.jeecommons.web.jsf.util.ResourceBundleUtil;
import ar.com.greensoft.neosystem.dao.GenericDao;
import ar.com.greensoft.neosystem.dao.PersonaFisicaDao;
import ar.com.greensoft.neosystem.model.Direccion;
import ar.com.greensoft.neosystem.model.PersonaFisica;
import ar.com.greensoft.neosystem.model.references.TipoDocumento;
import ar.com.greensoft.neosystem.search.SearchFilter;
import ar.com.greensoft.neosystem.search.TextSearchFilter;
import ar.com.greensoft.neosystem.util.CUITGenerator;

/**
 * @author Jorge E. Villaverde
 * @since 1.0
 */
@ManagedBean
@SessionScoped
@Named
public class PersonaFisicaBean extends PersonaBean<PersonaFisica> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3478606977660856105L;
	private static final String ENTITY_PREFIX = "personaFisica";
	
    @Inject
    private PersonaFisicaDao dao;
    	
	@Override
	protected GenericDao<PersonaFisica, Long> getDao() {
		return dao;
	}

	@Override
	protected PersonaFisica newEntity() {
		PersonaFisica entity = new PersonaFisica();
		entity.setTipoDocumento(TipoDocumento.DNI); 
		entity.setDireccion(new Direccion());
		return entity;
	}

	@Override
	protected Long getEntityId(PersonaFisica t) {
		return t.getId();
	}

	@Override
	protected String entityPrefix() {
		return ENTITY_PREFIX;
	}

	
	public void numeroDocumentoChangeListener(AjaxBehaviorEvent event) {
		PersonaFisica pf = dao.findByTipoNroDocumento(
				getEntity().getTipoDocumento(),
				getEntity().getNumeroDocumento());
		if(pf != null){
			setEntity(pf);
			setEditing(true);
			if(getEntity().getDireccion().getLocalidad() != null){
				this.provincia = getEntity().getDireccion().getLocalidad().getProvincia();
				this.pais = getEntity().getDireccion().getLocalidad().getProvincia().getPais();
			}
		}else{
			if(isEditing()){
				create();
				setEditing(false);
			}
		}
	}
	
	@Override
	protected String getEntityName(PersonaFisica entity) {
		return entity.getNombreApellido();
	}

	@Override
	protected PersonaFisica findEntityByName(String entityName) {
		return dao.findByName(entityName);
	}

	@Override
	protected List<ExportColInfo> getExportColInfoList() {
		List<ExportColInfo> ret = new ArrayList<ExportColInfo>();
		ret.add(new ExportColInfoImpl(1, "clienteNumeroDocumento"));
		ret.add(new ExportColInfoImpl(2, "personaFisicaApellido1"));
		ret.add(new ExportColInfoImpl(3, "personaFisicaApellido2"));
		ret.add(new ExportColInfoImpl(4, "personaFisicaPrimeroNombre"));
		ret.add(new ExportColInfoImpl(5, "personaFisicaSegundoNombre"));
		ret.add(new ExportColInfoImpl(6, "personaFisicaSexo"));
		return ret;
	}

	@Override
	protected ColumnValueMapper<PersonaFisica> getColumnValueMapper() {
		return new ColumnValueMapper<PersonaFisica>() {
			@Override
			public String getColumnValue(int columnNumber, PersonaFisica value) {
				switch (columnNumber) {
				case 0:
					return value.getCuit();
				case 1:
					return value.getApellido1();
				case 2:
					return value.getApellido2();
				case 3:
					return value.getPrimerNombre();
				case 4:
					return value.getSegundoNombre();
				case 5:
					return ResourceBundleUtil.getResourceBundleString(value.getSexo().getLabel(), HttpJSFUtil.getLocale());
				default:
					break;
				}
				return "";
			}
		};
	}

	@Override
	protected SearchFilter createSearchFilter() {
		return new TextSearchFilter();
	}

	@Override
	protected void beforeCreate() {
		super.beforeCreate();
		getEntity().setCuit(CUITGenerator.genertePersonaFisica(
				getEntity().getSexo(),
				getEntity().getNumeroDocumento()));
	}	
}